package com.sjht.android.bxgz;

import com.sjht.android.bxgz.utils.StringUtils;
import com.sjht.android.bxgz.utils.Utils;

import android.app.Activity;
import android.app.ActivityGroup;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;


public abstract class BaseActivity extends Activity implements OnClickListener, BxgzConstants {

	public BaseActivity() {
		mContext = this;
		mApp = BxgzApp.getSelf();
	}

	protected Context mContext;
	protected BxgzApp mApp;

	protected byte activityType = NAVIGATION_TYPE_SUB;

	protected boolean mIsFront = false;

	private BroadcastReceiver mExitAppReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			finish();
		}
	};

	protected static final int LAYOUT_TYPE_NORMAL = 0;
	protected static final int LAYOUT_TYPE_HEADER = 1;
	protected static final int LAYOUT_TYPE_PROGRESS = 2;
	protected static final int LAYOUT_TYPE_HEADER_PROGRESS = 3;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		if (mContext == this) {
			super.onCreate(savedInstanceState);
			if (!Utils.isNetworkAvailable(this)) {

			}
			registerReceiver(mExitAppReceiver, new IntentFilter(BxgzConstants.INTENT_ACTION_EXIT_APP));
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		mIsFront = true;
		if (activityType == NAVIGATION_TYPE_MAIN) {
			mApp.push(this);
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		mIsFront = false;
	}


	/**
	 * 处理Header右边按钮点击事件
	 * 
	 */
	protected void handleHeaderEvent() {
	}

	protected void setActivityType(byte _type) {
		activityType = _type;
	}

	/**
	 * <功能详细描述>是否响应物理按键到搜索，onKeyDown中调�?br>
	 * 
	 * @return<br>
	 */
	protected boolean isCanSearch() {
		return true;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_SEARCH) {
			if (!isCanSearch()) {
				return true;
			}
			return true;
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (event.getRepeatCount() == 0) {
				if (activityType != BxgzConstants.NAVIGATION_TYPE_MAIN) {
					if (this.getParent() instanceof ActivityGroup) {
						finish();
						getParent().finish();
					} else {
						finish();
					}

				} else {
					return getParent().onKeyDown(keyCode, event);
				}
			}

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onDestroy() {
		if (mContext == this) {
			unregisterReceiver(mExitAppReceiver);
		}

		if (null != mApp) {
			mApp.pull((Activity) mContext);
		}
		super.onDestroy();
	}

	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

	/**
	 * toast（默�?时间Toast.LENGTH_LONG�?
	 * 
	 * @param msg
	 *            内容
	 */
	protected void toast(String msg) {
		if (StringUtils.isNullOrEmpty(msg)) {
			return;
		}
		Utils.toast(mContext, msg, mIsFront);
	}

	/**
	 * toast（默�?时间Toast.LENGTH_LONG�?
	 * 
	 * @param msg
	 *            内容
	 * @param duration
	 *            时间
	 */
	protected void toast(int resid) {
		Utils.toast(mContext, resid, mIsFront);
	}

	/**
	 * toast
	 * 
	 * @param resid
	 *            内容资源id
	 * @param duration
	 *            时间
	 */
	protected void toast(String msg, int duration) {
		if (StringUtils.isNullOrEmpty(msg)) {
			return;
		}
		Utils.toast(mContext, msg, mIsFront, duration);
	}

	protected void toast(int resid, int duration) {
		Utils.toast(mContext, resid, mIsFront, duration);
	}

}
